package com.yanqu.road.dao.impl.activity.peakstronghold;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.peakstronghold.CrossUnionAbilityCacheData;
import com.yanqu.road.entity.activity.peakstronghold.CrossUnionAbilityRankData;
import com.yanqu.road.entity.union.UnionBaseInfo;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossUnionAbilityRankDataDaoImpl extends TempDataObjectDao<CrossUnionAbilityRankData> {
    public static String getCrossUnionAbilityRankDataSql = "select * from t_c_union_ability_rank where activityId=?";

    public List<CrossUnionAbilityRankData> getCrossUnionAbilityRankData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<CrossUnionAbilityRankData> dataList = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossUnionAbilityRankDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossUnionAbilityRankData data = parse(rs);
                dataList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    public static String updateCrossUnionAbilityRankDataSql = "update t_c_union_ability_rank set unionId=?,unionBaseInfo=?,ability=?,lastUpdateTime=?,rank=? where activityId=? and rankId=?";

    @Override
    protected boolean updateData(CrossUnionAbilityRankData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getUnionBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getAbility().toString()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRank()));

        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRankId()));
        if (getDb().executeNoneQuery(updateCrossUnionAbilityRankDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossUnionAbilityRankDataSql = "insert into t_c_union_ability_rank(activityId,rankId,unionId,unionBaseInfo,ability,lastUpdateTime,rank) values (?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(CrossUnionAbilityRankData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRankId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getUnionBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getAbility().toString()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRank()));

        if (getDb().executeNoneQuery(insertCrossUnionAbilityRankDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossUnionAbilityRankData parseData(ResultSet rs) throws SQLException {
        CrossUnionAbilityRankData data = new CrossUnionAbilityRankData();
        data.setActivityId(rs.getInt("activityId"));
        data.setRankId(rs.getInt("rankId"));
        data.setUnionId(rs.getString("unionId"));
        data.setUnionBaseInfo(JSON.parseObject(rs.getString("unionBaseInfo"), new TypeReference<UnionBaseInfo>() {
        }));
        data.setAbility(new BigInteger(rs.getString("ability")));
        data.setLastUpdateTime(rs.getLong("lastUpdateTime"));
        data.setRank(rs.getInt("rank"));
        return data;
    }
}
